package cn.tedu.em.passport.controller;

import cn.tedu.em.common.pojo.passport.dto.AdminAddNewDTO;
import cn.tedu.em.common.pojo.passport.dto.AdminLoginInfoDTO;
import cn.tedu.em.common.pojo.passport.vo.AdminListItemVO;
import cn.tedu.em.common.pojo.passport.vo.AdminStandardVO;
import cn.tedu.em.passport.security.LoginPrincipal;
import cn.tedu.em.passport.service.IAdminService;
import cn.tedu.em.passport.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admins")
@Api(tags = "01. 用户管理模块")
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class AdminController {

    @Autowired
    private IAdminService adminService;

    public AdminController(){
        log.debug("创建控制器类对象: AdminController");
    }

    //http://localhost:9998/admins/login
    @PostMapping("/login")
    @ApiOperation("管理员登录")
    @ApiOperationSupport(order = 10)
    public JsonResult<String> login(AdminLoginInfoDTO adminLoginInfoDTO){
        log.debug("开始处理【用户登录】的请求, 参数: {}",adminLoginInfoDTO);
        String jwt = adminService.login(adminLoginInfoDTO);
        return JsonResult.ok(jwt);
    }

    //http://localhost:9998/admins/add-new
    @PostMapping("/add-new")
    @ApiOperation("添加管理员")
    @ApiOperationSupport(order = 100)
    @PreAuthorize("hasAuthority('/ams/admin/add-new')")
    public JsonResult<Void> addNew(AdminAddNewDTO adminAddNewDTO){
        log.debug("开始处理【添加管理员】");
        adminService.addNew(adminAddNewDTO);
        return JsonResult.ok();
    }

    //http://localhost:9998/admins/9527/delete
    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除用户")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    })
    @PreAuthorize("hasAuthority('/ams/admin/delete')")
    public JsonResult<Void> delete(@PathVariable @Range(min = 1, message = "请提交有效的ID值!") Long id){
        log.debug("开始处理【根据ID删除用户】的请求，参数：{}", id);
        adminService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:9999/admins/9527/enable
    @PostMapping("/{id:[0-9]+}/enable")
    @ApiOperation("启用用户")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    })
    public JsonResult<Void> setEnable(@PathVariable @Range(min = 1, message = "请提交有效的ID值!") Long id) {
        log.debug("开始处理【根据ID启用用户】的请求，参数：{}", id);
        adminService.setEnable(id);
        return JsonResult.ok();
    }

    // http://localhost:9999/admins/9527/disable
    @PostMapping("/{id:[0-9]+}/disable")
    @ApiOperation("禁用用户")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    })
    public JsonResult<Void> setDisable(@PathVariable @Range(min = 1, message = "请提交有效的ID值!") Long id) {
        log.debug("开始处理【根据ID禁用用户】的请求，参数：{}", id);
        adminService.setDisable(id);
        return JsonResult.ok();
    }

    //http://localhost:9998/admins/9527
    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询用户详情")
    @ApiOperationSupport(order = 410)
    @PreAuthorize("hasAuthority('/ums/admin/read')")
    public JsonResult<AdminStandardVO> getStandardById(
            @PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id){
        log.debug("开始处理【根据ID查询用户详情】的请求，参数：{}", id);
        AdminStandardVO queryResult = adminService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    //http://localhost:9998/admins
    @GetMapping("")
    @ApiOperation("查询用户列表")
    @ApiOperationSupport(order = 420)
    @PreAuthorize("hasAuthority('/ams/admin/read')")
    public JsonResult<List<AdminListItemVO>> list(@ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal){
        log.debug("开始处理【用户列表】的请求,无参数");
        log.debug("当事人信息: {}",loginPrincipal);
        log.debug("当事人信息中的ID: {}",loginPrincipal.getId());
        log.debug("当事人信息中的用户名: {}",loginPrincipal.getUsername());
        List<AdminListItemVO> list = adminService.list();
        return JsonResult.ok(list);
    }
}
